import React from "react";
import {Button, Modal, Menu,Table, ConfigProvider, Divider} from "antd/lib";
import ContractAmount from "@/services/contractamount";
import CreateAmountDialog from "@/components/contract/new/AmountDialog";
import notification from "@/services/notification";
import {get} from "lodash";
import zhCN from 'antd/es/locale/zh_CN';
import GlobalSetting from "@/services/globalsetting";
import { find } from "lodash";
import { Query } from "@/services/query";
import VisualizationRenderer from "@/components/visualizations/VisualizationRenderer";
import WarningOutlined from "@ant-design/icons/WarningOutlined";

const { confirm } = Modal;
export default class FinishHrp extends React.Component {
    state = {
    loading: false,
    queryid:0,
    visualizationid:0,
    query:null,
    visualization:null,
    queryresults:null,
    error:null,
  };
 componentWillMount() {
    let param={
    page: 1,
    pagesize: 5,
    code: 'HRP合同资料',
    type: '合同验收',
  }
  GlobalSetting.getlist(param).then(data => {
    const ids = data.data[0].value.split(',');
    const qid = Number(ids[0]);
    let vid= ids.length>1 ? Number(ids[1]):0;
    if(qid>0){
      Query.get({ id: qid })
      .then(result => {
          let vistion=result.visualizations[0];
          if (vid>0){
              vistion = find(result.visualizations, vis => vis.id === vid);
          }
          if(result.options.parameters.length>0){
            result.options.parameters[0].value=this.props.contractcode;
          }
          this.setState({
            queryid:qid,
            visualizationid:vid,
            query:result,
            visualization:vistion,
            error:null,
          });
          this.refreshQueryResults(result);
      })
      .catch(error => {
        this.setState({
          error: error.message +":" + error.detailedMessage,
        });
      });
    }
  });
  }
  refreshQueryResults = (query) =>{
    if (query) {
      query
        .getQueryResultPromise()
        .then(result => {
          this.setState({
            queryresults:result,
            error:null,
          });
        })
        .catch(error => {
          this.setState({
            error: error.message +":" + error.detailedMessage,
          });
        });
    }
  };
  render() {
    if (this.state.loading) return null;
    return (
        <div>
            {this.state.error && <div className="alert alert-danger" data-test="ErrorMessage">{`Error: ${this.state.error}`}</div>}
            {!this.state.error && this.state.queryresults && (
              <VisualizationRenderer 
              visualization={this.state.visualization} 
              queryResult={this.state.queryresults} context="query" />
            )}
        </div>
    );
    }
}
